﻿#include "file_manager_arr.h"

CFile_manager_arr::CFile_manager_arr()
{
	m_vec_file_man.reserve(2);
	m_vec_file_man.clear();
	m_chNum = 1;
}

CFile_manager_arr::~CFile_manager_arr()
{
}

int CFile_manager_arr::Init(int chNum)
{
	int ret = 0;

	m_chNum = chNum;

	return ret;
}

int CFile_manager_arr::GetRecorInfo()
{
	int ret = 0;

	return ret;
}

int CFile_manager_arr::Reset()
{
	int ret = 0;

	return ret;
}

int CFile_manager_arr::InitSorageDir(char * storageName,RECORD_DIR_INFO_S recDir)
{
	int ret = 0;

	if (0== m_vec_file_man.size())
	{
		CFile_manager * pFileManager = new CFile_manager();

		ret = pFileManager->Init(storageName,m_chNum,recDir);

		m_vec_file_man.push_back(pFileManager);
	}
	else
	{
		for(int i = 0; i < (int)m_vec_file_man.size(); i++)
		{
			m_vec_file_man[0]->Init(storageName,m_chNum,recDir);
		}

	}
//	m_vec_file_man.push_back(pFileManager);
	TRACE("AddFileManager %s  mnt path %s .\n",storageName,recDir.df_mntPath);

	return ret;
}

int CFile_manager_arr::AddFileManager(char * storageName,RECORD_DIR_INFO_S recDir)
{
	int ret = 0;
	CFile_manager * pFileManager = new CFile_manager();
	
	m_lock.Lock();

	ret = pFileManager->Init(storageName,m_chNum,recDir);
	m_vec_file_man.push_back(pFileManager);
	
	m_lock.UnLock();

    TRACE("AddFileManager %s  mnt path %s .\n",storageName,recDir.df_mntPath);

	return ret;
}

void CFile_manager_arr::RemoveFileManager(char * storageName)
{
	vector<CFile_manager*>::iterator itIdx;

	m_lock.Lock();
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		if(strcmp((*itIdx)->get_device_name(), storageName)== 0)
		{
			delete [] * itIdx;
			m_vec_file_man.erase(itIdx);
		}
	}
	m_lock.UnLock();

}

int CFile_manager_arr::Close()
{
	int ret = 0;
	return ret;
}

/*
功能：获取录像文件
filesize : 文件大小

*/
int CFile_manager_arr::GetNextRecIndex(uint32_t channel,uint16_t & diskIndex ,string & fileName, uint32_t & fileIndex, uint32_t & fileSize,time_t recTm,int recType)
{
	int ret = 0;
	int i = 0;
	bool bGet = 0;
	uint32_t free_MB = 0;

	// 支持多个SD卡管理
	//TRACE("Get next record index CH%02d file_man %d\n",channel,m_vec_file_man.size());

	for(i = 0; i < (int)m_vec_file_man.size();i++)
	{
		//剩余空间大小大于16M以上
		free_MB = m_vec_file_man[i]->GetRecFreeSpaceMB();
		if(recType == RECORD_IOALARM)
		{
			if( m_vec_file_man[i]->GetDiskFreeSpaceMB() < LITTLE_SIZE_MB)
			{
				continue;
			}
		}
		else
		{
			if( free_MB < LITTLE_SIZE_MB )
			{
		//		TRACE("free_MB %d is too little.\n",free_MB);
				continue;
			}
		}


        diskIndex = i;
		ret = m_vec_file_man[i]->GetNexRecordFile(channel,fileName,fileIndex,fileSize,recTm,recType);
		if(free_MB != 0 && free_MB < BYTE_TO_MB(fileSize))
		{
			fileSize = SIZE_MB(free_MB);
			TRACE("last free_MB %d file size %d .\n",free_MB,BYTE_TO_MB(fileSize));
		}

		bGet = true;
		break;
	}

	if(!bGet)
	{
		if(recType == RECORD_IOALARM)
		{   //空间不足
			return ERR_SPACE_NOT_ENOUGH;
		}
		return ERR_INDEX_NORECIDX;
	}

	return ret;
}

//回收空间
int CFile_manager_arr::CycleRecord()
{
	int ret = 0;


	return ret;
}

int CFile_manager_arr::RecordStart(REC_FILE_INFO & recInfo)
{
	int ret = 0;

	if(recInfo.diskIndex > m_vec_file_man.size())
	{
		TRACE_ERR("Error disk index %d .\n",recInfo.diskIndex);
		recInfo.diskIndex  = 0;
	}

	if(1 == m_vec_file_man.size())
	{
		return m_vec_file_man[0]->RecordStart(recInfo);
	}


	ret = m_vec_file_man[recInfo.diskIndex]->RecordStart(recInfo);
	return ret;
}

//停止录像
int CFile_manager_arr::RecordEnd(REC_FILE_INFO & recInfo)
{
	int ret = 0;

	if(recInfo.diskIndex > m_vec_file_man.size())
	{
		TRACE_ERR("Error disk index %d .\n",recInfo.diskIndex);
		recInfo.diskIndex  = 0;
	}

	if(1 == m_vec_file_man.size())
	{
		return m_vec_file_man[0]->RecordEnd(recInfo);
	}


	ret = m_vec_file_man[recInfo.diskIndex]->RecordEnd(recInfo);

	return ret;
}


int CFile_manager_arr::LockFile(REC_FILE_INFO & recInfo)
{
	int ret = 0;

	return ret;
}

int CFile_manager_arr::UnlockFile(REC_FILE_INFO & recInfo)
{
	int ret = 0;

	return ret;
}

CFile_manager* CFile_manager_arr::GetDiskFileManager(int diskNo)
{
	int ret = 0;

	return NULL;
}

void CFile_manager_arr::SetRecordSize(uint32_t fileSize)
{
	vector<CFile_manager*>::iterator itIdx;
	int recNum = 0;
	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		(*itIdx)->SetRecFileSize(fileSize);
	}
}


int CFile_manager_arr::SearchRecord(uint64_t tm_start,int64_t tm_end, int recType,vector<uint8_t>& vecCH,uint32_t maxNum,MAP_CH_FILE_T& mapPerChFile,bool bForward)
{
	int recNum = 0;
	vector<CFile_manager*>::iterator itIdx;

	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		recNum += (*itIdx)->SearchFile(tm_start,tm_end,recType,vecCH,maxNum,mapPerChFile,bForward);
	}

	if(recNum > 0)
	{
		return recNum;
	}
	return ERR_INDEX_NORECFILE;
}

uint32_t CFile_manager_arr::GetNextRecordSN()
{
	if(0 == m_vec_file_man.size())
	{
		return ERR_NO_DEVICE;
	}
	//优先从上次的磁盘号再开始选择录像文件
	return m_vec_file_man[m_curDiskNo]->GetNextRecordSN();
}

void CFile_manager_arr::GetMaxTime(time_t &maxTm)
{
	time_t tmpTime = 0;

	maxTm = 0;
	for(int i = 0; i < (int)m_vec_file_man.size(); i++)
	{
		m_vec_file_man[i]->GetMaxTime(tmpTime);

		if(tmpTime > maxTm )
		{
			maxTm = tmpTime;
		}
	}

}

int CFile_manager_arr::RegisterRecWriter(int diskIdx, int ch, CRecWriteBase * pRec)
{
	if( 0== m_vec_file_man.size())
	{
		return 0;
	}
	if( 1 == m_vec_file_man.size())
	{
		diskIdx = 0;
	}
	for(int i = 0; i < (int)m_vec_file_man.size();i++)
	{
		m_vec_file_man[i]->RegisterRecWriter(ch,pRec);
	}
	return 0;
}

int CFile_manager_arr::DestroyRecWriter(int diskIdx,int ch)
{
    return 0;
}


int CFile_manager_arr::GetLeafRecNum()
{

	return 0;
}

int CFile_manager_arr::GetLeafFileNum()
{

	return 0;
}

int CFile_manager_arr::GetDiskNum()
{
	return	(int )m_vec_file_man.size();
}

uint32_t CFile_manager_arr::GetFreeSpaceMB()
{
	int      ret        = 0;
	uint32_t freeSizeMB = 0;

	vector<CFile_manager*>::iterator itIdx;

	//优先从盘组当中获取录像文件
	CCross_LockAuto tempLock(&m_lock);

	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		freeSizeMB += (*itIdx)->GetRecFreeSpaceMB();
	}
	return freeSizeMB;
}

int CFile_manager_arr::GetDataPercent()
{
	return 0;
}

//循环录像时，当录像索引已经用完时，需要进行回收
int CFile_manager_arr::GetCycleIndex(uint32_t channel,string & filename,uint16_t & diskIndex, uint32_t & indexNo,uint32_t & fileLen,time_t recTm)
{
	int ret = 0;
	vector<CFile_manager*>::iterator itIdx;

	//优先从盘组当中获取录像文件
	CCross_LockAuto tempLock(&m_lock);

	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		ret = (*itIdx)->GetCycleIndex(channel,filename,recTm,diskIndex,indexNo);
		if(ret >= 0)
		{
			indexNo = ret;
			fileLen = (*itIdx)->GetRecFileSize();
			return ret;
		}
	}

	return ERR_INDEX_NORECIDX;
}

int CFile_manager_arr::ScanDevice()
{
	int idx = 0;
	char device[32] = {0};
	char dev_no = 0;
	//扫描SD卡，设计最多支持4张SD卡
	for(idx = 0; idx < 4; idx++)
	{
		sprintf(device,"/dev/mmcblock%d",idx);
		if(access(device,0) != 0)
		{
			continue;
		}

		//设备存在的情况下
	}

	//扫描磁盘
	for(idx = 0; idx < 16; idx++)
	{
		dev_no = 'a' + idx;
		sprintf(device,"/dev/sd%d",dev_no);
		if(access(device,0) != 0)
		{
			continue;
		}

	}

	return 0;
}

char * CFile_manager_arr::GetDiskName(int diskIndex)
{
	int ret = 0;
	int i = 0;
	bool bGet = 0;

	if(diskIndex >= m_vec_file_man.size())
	{
		return NULL;
	}
	return m_vec_file_man[diskIndex]->GetDiskName();
}

int CFile_manager_arr::GetNextPic(string & picFile,time_t curTm, int channel)
{
	int ret = 0;
	vector<CFile_manager*>::iterator itIdx;

	//优先从盘组当中获取录像文件
	CCross_LockAuto tempLock(&m_lock);

	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		ret = (*itIdx)->GetNextPic(picFile,curTm,channel);
		if(ret >= 0)
		{
			return ret;
		}
	}

	return ERR_INDEX_NORECIDX;
}

void CFile_manager_arr::SetNorSpace(uint32_t space)
{
	int ret = 0;
	vector<CFile_manager*>::iterator itIdx;

	//优先从盘组当中获取录像文件
	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		(*itIdx)->SetNorSpace(space);
	}
}

void CFile_manager_arr::SetNorSpace(uint32_t space,char * dev)
{
	int ret = 0;
	vector<CFile_manager*>::iterator itIdx;

	//优先从盘组当中获取录像文件
	//逐个遍历每个磁盘
	for(itIdx = m_vec_file_man.begin(); itIdx != m_vec_file_man.end(); itIdx++)
	{
		if(strcpy((*itIdx)->GetDiskName(),dev))
		{
			(*itIdx)->SetNorSpace(space);
			break;
		}
	}
}

int CFile_manager_arr::GetFilePath(char * fileName, FILE_INFO & fInfo)
{
	int ret = 0;
	int diskIdx = fInfo.diskIndex;
	
	if(m_vec_file_man.size() == 1)
	{
		return  m_vec_file_man[0]->GetFilePath(fileName,fInfo);
	}

	ret  = m_vec_file_man[diskIdx]->GetFilePath(fileName,fInfo);

	return ret;
}
